<?php

namespace app\merchant\controller\apps\sharp;

use app\merchant\controller\Base;
use app\merchant\model\sharp\Active as ActiveModel;
use app\merchant\model\sharp\ActiveTime as ActiveTimeModel;
use app\merchant\model\sharp\ActiveGoods as ActiveGoodsModel;

/**
 * 秒杀活动会场-场次管理
 * Class ActiveTime
 * @package app\merchant\controller\apps\sharp
 */
class ActiveTime extends Base
{
    /**
     * 活动会场-场次列表
     * @param $active_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->postData();
        if(!isset($param['active_id']) || isInt($param['active_id'])){
            return $this->renderError([],"参数错误");
        }

        $model = new ActiveTimeModel;
        $list = $model->getList($param['active_id']);
        return $this->renderSuccess($list,'查询成功');
    }


    /**
     * 新增场次获取活动会场时间
     * @param $active_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function getActiveData()
    {
        $param = $this->postData();
        if(!isset($param['active_id']) || isInt($param['active_id'])){
            return $this->renderError([],"参数错误");
        }
        // 活动详情
        $active = ActiveModel::detail($param['active_id']);
        // 已存在的场次
        $model = new ActiveTimeModel;
        $existTimes = $model->getActiveTimeData($param['active_id']);

        return $this->renderSuccess(compact('active', 'existTimes'),'查询成功');

    }

    /**
     * 新增活动会场
     * @param $active_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $param = $this->postData();
        if(!isset($param['active_id']) || isInt($param['active_id'])){
            return $this->renderError([],"参数错误");
        }
        $model = new ActiveTimeModel;
        // 新增记录
        if ($model->add($param['active_id'], $this->postData())) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '添加失败');
    }



    /**
     * 活动会场详情
     * @param $id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function detail()
    {
        $param = $this->postData();
        if(!isset($param['id']) || isInt($param['id'])){
            return $this->renderError([],"参数错误");
        }
        // 场次详情
        $model = ActiveTimeModel::detail($param['id'], ['active']);
        if(!$model){
            return $this->renderError([],"场次不存在");
        }
        // 当前场次关联的商品
        $goodsList = $model->getGoodsListByActiveTimeId($param['id']);

        return $this->renderError(compact('model', 'goodsList'),"查询成功");

    }

    /**
     * 编辑活动会场
     * @param $id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function edit()
    {
        $param = $this->postData();

        if(!isset($param['id']) || isInt($param['id'])){
            return $this->renderError([],"参数错误");
        }
        // 场次详情
        $model = ActiveTimeModel::detail($param['id'], ['active']);
        if(!$model){
            return $this->renderError([],"场次不存在");
        }

        // 编辑记录
        if ($model->edit($param)) {

            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError([],$model->getError() ?: '更新失败');
    }

    /**
     * 修改场次状态
     * @param $id
     * @param $state
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function state()
    {
        $param = $this->postData();

        if(!isset($param['id']) || isInt($param['id'])){
            return $this->renderError([],"参数错误");
        }
        // 场次详情
        $model = ActiveTimeModel::detail($param['id'], ['active']);
        if(!$model){
            return $this->renderError([],"场次不存在");
        }

        if (!$model->setStatus($param['status'])) {
            return $this->renderError([],'操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }

    /**
     * 删除活动场次
     * @param $id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function delete()
    {
        $param = $this->postData();

        if(!isset($param['id']) || isInt($param['id'])){
            return $this->renderError([],"参数错误");
        }
        // 场次详情
        $model = ActiveTimeModel::detail($param['id'], ['active']);
        if(!$model){
            return $this->renderError([],"场次不存在");
        }

        if (!$model->onDelete()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

}